﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QuickChat_Service.Controllers;
using QuickChat_Service.Repositories.Database;
using QuickChat_Service.Repositories;
using System.Linq;

namespace QuickChat_Service.Tests.Controllers
{
    [TestClass]
    public class TalkersControllerTest
    {
        [TestMethod]
        public void Get()
        {
            // Arrange
            TalkersController controller = new TalkersController(new FakeChatServiceUnitOfWork());

            // Act
            IQueryable<Talker> result = controller.Get();

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(5, result.Count());
            Assert.AreEqual("Talker3", result.ElementAt(2).ChatAlias);
            Assert.AreEqual(2, result.ElementAt(1).TalkerId);
        }

        [TestMethod]
        public void GetById()
        {
            // Arrange
            TalkersController controller = new TalkersController(new FakeChatServiceUnitOfWork());

            // Act
            bool result = controller.Get(1).IsSuccessStatusCode;

            //Talker expected = new Talker() { TalkerId = 2, ChatAlias = "Talker2", CurrentRoomId = 2 };

            bool expected = true;

            // Assert
            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void Post()
        {
            // Arrange
            TalkersController controller = new TalkersController(new FakeChatServiceUnitOfWork());

            int expected = controller.Get().Count() + 1;

            // Act
            controller.Post(new Talker() { TalkerId = 6, ChatAlias = "Talker6", CurrentRoomId = 2 });

            int result = controller.Get().ToList().Count();

            // Assert
            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void Put()
        {
            // Arrange
            TalkersController controller = new TalkersController(new FakeChatServiceUnitOfWork());

            // Act
            controller.Put(new Talker() { TalkerId = 6, ChatAlias = "Talker6", CurrentRoomId = 3 });

            // Assert
            Assert.IsFalse(controller.Get().Any(t => t.CurrentRoomId == 2));
            Assert.IsTrue(controller.Get().Any(t => t.CurrentRoomId == 3));
        }

        [TestMethod]
        public void Delete()
        {
            // Arrange
            TalkersController controller = new TalkersController(new FakeChatServiceUnitOfWork());

            int expected = controller.Get().Count() - 1;

            // Act
            controller.Delete(2);

            int result = controller.Get().Count();
            
            // Assert
            //Assert.IsFalse(controller.Get().Count(t => t.TalkerId == 2) > 0);
            Assert.AreNotEqual(expected, result);
        }
    }
}
